<template>
	<view>
		<slot></slot>
		<view class="mask" v-if="visible" @touchmove="moveHandle">
			<view class="picker-date">
				<view class="button">
					<view @click="changeVisible">取消</view>
					<view @click="confirm" :class="disable ? '' : 'disable'">确认</view>
				</view>
				<picker-view indicator-style="height: 50px; line-height: 50px;" :value="value" @change="bindChange" class="picker-view">
					<picker-view-column v-for="(item, index) in range" :key="index">
						<view class="item" v-for="(item1, index1) in item" :key="index1">{{ item1.label }}</view>
					</picker-view-column>
				</picker-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ChooseSite',
		props: {
			visible: {
				type: Boolean,
				required: true
			},
			list: {
				type: Array,
				required: true
			}
		},
		data() {
			return {
				range: [],
				value: [0, 0, 0],
				oldValue: [0, 0, 0],
				disable: true
			};
		},
		watch: {
			list: {
				handler(val) {
					if (val.length) {
						this.editRange();
					}
				},
				immediate: true
			},
			value(val, oldVal) {
				if (val[0] !== oldVal[0]) {
					this.changeTwo(this.range[0][val[0]].label);
					this.$set(this.value, 1, 0);
					this.$set(this.value, 2, 0);
					this.changeThree(this.range[0][val[0]].label, this.range[1][val[1]].label);
				} else if (val[1] !== oldVal[1]) {
					this.changeThree(this.range[0][val[0]].label, this.range[1][val[1]].label);
					this.$set(this.value, 2, 0);
				}
			}
		},
		methods: {
			moveHandle(e) {
				e.preventDefault();
				e.stopPropagation();
			},
			// 数据项发生改变
			bindChange(e) {
				this.disable = false;
				this.value = e.detail.value;
				this.disable = true;
			},
			// 弹框隐藏
			changeVisible() {
				this.$emit('update:visible', !this.visible);
				// this.changeValue();
				this.value = this.oldValue;
			},
			// 确认
			confirm() {
				if (this.disable) {
					this.$emit('update:visible', !this.visible);
					this.oldValue = this.value;
					const obj = {
						station_area_id: this.range[0][this.value[0]].id,
						station_subarea: this.range[0][this.value[0]].label,
						station_floor_num: this.range[1][this.value[1]].id,
						station_floor_alias: this.range[1][this.value[1]].label,
						station_id: this.range[2][this.value[2]].id,
						station_num: this.range[2][this.value[2]].label,
					}
					const title = `${this.range[0][this.value[0]].label}-${this.range[1][this.value[1]].label}-${this.range[2][this.value[2]].label}`
					this.$emit('change', obj, title);
					
				}
			},
			changeTwo(label) {
				const two = [];
				this.list.forEach(item => {
					if (item.station_subarea === label) {
						if (two.every(i => i.label !== item.station_floor_alias)) {
							two.push({
								id: item.station_floor_num,
								label: item.station_floor_alias
							})
						}
					}
				})
				this.$set(this.range, 1, two);
				return two[0].label;
			},
			changeThree(oLabel, tLabel) {
				const three = [];
				this.list.forEach(item => {
					if (item.station_subarea === oLabel && item.station_floor_alias === tLabel) {
						if (three.every(i => i.label !== item.station_num)) {
							three.push({
								id: item.station_id,
								label: item.station_num
							})
						}
					}
				})
				this.$set(this.range, 2, three);
			},
			editRange() {
				const one = [];
				this.list.forEach(item => {
					if (one.every(i => i.label !== item.station_subarea)) {
						one.push({
							id: item.station_area_id,
							label: item.station_subarea
						})
					}
				})
				this.$set(this.range, 0, one);
				const Tlabel = this.changeTwo(one[0].label);
				this.changeThree(one[0].label, Tlabel);
			}
		}
	}
</script>

<style scoped lang="scss">
	.disable {
		color: #999988;
	}
	.mask {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		width: 100% !important;
		height: 100% !important;
		background-color: rgba(0, 0, 0, 0.4);
		
		z-index: 9999;
	}
	.picker-date {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		height: 600rpx;
		background-color: #FFFFFF;
	}
	.button {
		display: flex;
		justify-content: space-between;
		align-items: center;
		
		padding: 0 50rpx;
		height: 80rpx;
		
		font-size: 32rpx;
		color: $uni-text-color;
		background-color: #F8F8F8;
		
		box-sizing: border-box;
		:nth-child(2) {
			color: $uni-color-primary;
		}
	}
	.picker-view {
	    width: 750rpx;
	    height: 520rpx;
	}
	.item {
	    height: 50px;
		line-height: 50px;
	    align-items: center;
		color: #000000;
	    justify-content: center;
	    text-align: center;
	}
</style>
